package thrift;

import com.winwill.thrift.HelloWordService;
import com.winwill.thrift.Request;
import com.winwill.thrift.RequestType;
import org.apache.thrift.TException;
import org.apache.thrift.protocol.TBinaryProtocol;
import org.apache.thrift.protocol.TMultiplexedProtocol;
import org.apache.thrift.protocol.TProtocol;
import org.apache.thrift.transport.TSocket;
import org.apache.thrift.transport.TTransport;
import org.apache.thrift.transport.TTransportException;

public class MulityServiceClient {
    public static void main(String[] args) {
        try {
            TTransport transport = new TSocket("localhost", 9090);
            TProtocol protocol = new TBinaryProtocol(transport);
            //通过TMultiplexedProtocol获取对应的服务
            TMultiplexedProtocol protocolTest = new TMultiplexedProtocol(protocol, "testService");
            TMultiplexedProtocol protocolHello = new TMultiplexedProtocol(protocol, "helloService");

            //创建Client调用服务接口的方法
            TestService.Client testClient = new TestService.Client(protocolTest);
            HelloWordService.Client clientHello = new HelloWordService.Client(protocolHello);
            transport.open();

            //调用接口方法
            Test test = new Test();
            test.setBool_(true);
            test.setDouble_(5.0);
            System.out.println("Client getBool()" + testClient.getBool(test));
            System.out.println("Client getDouble()" + testClient.getDouble(test));

            Request request = new Request(RequestType.QUERY_TIME, "Jerry");
            System.out.println("HelloService sayString :" + clientHello.doAction(request));

            transport.close();
        } catch (TTransportException e) {
            e.printStackTrace();
        } catch (TException te) {
            te.printStackTrace();
        }
    }
}
